//
//  ViewController.swift
//  CoreSpotlightDemo
//
//  Created by 李禹 on 15/12/27.
//  Copyright © 2015年 dayu. All rights reserved.
//

import UIKit
import CoreSpotlight

let kText = "text"
let kSubTitle = "subTitle"
let kImage = "image"

class ViewController: UITableViewController {
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "删除", style: UIBarButtonItemStyle.Done, target: self, action: "deleteSpotlight");
        saveData();
    }
    
    func deleteSpotlight() {
        CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers(["First"], completionHandler: nil);
        CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithDomainIdentifiers(["com.dayu.CoreSpotlightDemo.Second"], completionHandler: { (error) -> Void in
            print("\(error?.localizedDescription)");
        });

        
        CSSearchableIndex.defaultSearchableIndex().deleteAllSearchableItemsWithCompletionHandler { (error) -> Void in
            print("\(error?.localizedDescription)");
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    
    lazy var dataList: [[String: String]] = {
        let firstItem = [kText: "First", kSubTitle: "Number1"];
        let secondItem = [kText: "Second", kSubTitle: "No2"];
        let thirdItem = [kText: "Third", kSubTitle: "Three"];
        
        let list = [firstItem, secondItem, thirdItem];
        return list;
    }()

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if ((segue.identifier?.hasSuffix("2Detail")) != nil) , let indexPath = self.tableView.indexPathForSelectedRow {
            let cell = self.tableView.cellForRowAtIndexPath(indexPath);
            let secondeVC = segue.destinationViewController as UIViewController;
            secondeVC.title = cell?.textLabel?.text;
        }
    }
    
    func showViewControllerWithIdentifier(identifier: String) {
//        switch identifier {
//            case "First":
//                performSegueWithIdentifier("First2Detail", sender: self);
//            case "Second":
//                performSegueWithIdentifier("Second2Detail", sender: self);
//            case "Third":
//                performSegueWithIdentifier("Third2Detail", sender: self);
//            default:
//                performSegueWithIdentifier("Third2Detail", sender: self);
//                break
//            
//        }
    }

    func saveData() {
        var searchableItems = [CSSearchableItem]();
        
        for idx in 0..<dataList.count {
            let attributeSet = CSSearchableItemAttributeSet(itemContentType: "views");
            
            let info = dataList[idx];
            let title = info[kText];
            
            attributeSet.title = title;
            attributeSet.contentDescription = "\(info[kSubTitle])";
            
            let imgName = "icon_\(title!)";
            let thumbImage = UIImage(named: imgName);
            attributeSet.thumbnailData = UIImagePNGRepresentation(thumbImage!);
            
            let item = CSSearchableItem(uniqueIdentifier: title, domainIdentifier: "com.dayu.CoreSpotlightDemo.\(title)", attributeSet: attributeSet);
            searchableItems.append(item);
        }
        
        CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in
            if error != nil {
                print(error!.localizedDescription);
            }
        }
    }

}

